import { _login } from "../../../service/api";

Page({
  data: {
    userInfo: {},
  },
  onLoad: function (options) {
    const userInfo = wx.getStorageSync("userInfo");
    this.setData({ userInfo });
  },
  // 点击登录
  async handleLogin() {
    // const { code } = await wx.login();
    // console.log(code);
    // const result = await _login();
    // console.log(result);
    // return;
    const token = wx.getStorageSync("token") || "";
    if (!token) {
      try {
        const { userInfo } = await wx.getUserProfile({ desc: "登录" });
        const { code } = await wx.login();
        const result = await _login({ code, userInfo });
        console.log(result);
        if (parseInt(result.errno) !== 0) {
          return wx.showToast({ title: "微信登录失败" });
        } else {
          this.setData({ userInfo });
          wx.setStorageSync("token", result.token);
          wx.setStorageSync("userInfo", userInfo);
        }
      } catch (err) {
        console.log(err);
      }
    }
  },
});
